package cn.qyd.threadpool;

import org.checkerframework.checker.units.qual.C;

import java.util.Arrays;
import java.util.concurrent.*;

/**
 * @author 邱运铎
 * @date 2024-03-14 14:11
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "Hello From " + Thread.currentThread().getName();
            }
        };
        Future<String>[] futures = new Future[10];
        for (int i = 0; i < 10; i++) {
            futures[i] = executorService.submit(callable);
        }

        Arrays.stream(futures).forEach(future -> {
            try {
                System.out.println(future.get());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        });

        executorService.shutdown();
    }
}
